var testn;
if(!testn){
	testn =1;
}
console.log(testn)
var ObjFun={
	inits:function(){
		this.ajax();  //初始化ajax请求
		this.DaohangeventFun();
		this.DetailsFun();
	},
	Interface:{
		goodsDaohang:"http://ww.yc630.com/api/index.php/Buyer/Goods/goodsDaohang",  //商品导航
		goodsList:"http://ww.yc630.com/api/index.php/Buyer/Goods/goodsList",  //商品列表
		getProvince:"http://ww.yc630.com/api/index.php/Buyer/User/getProvince", //获取省
		getCity:"http://ww.yc630.com/api/index.php/Buyer/User/getCity",   //获取市
		getArea:"http://ww.yc630.com/api/index.php/Buyer/User/getArea"  //获取区县
	},
	goodsListObj:{
		people_style:common.Geturl().people_style,
		goods_style:common.Geturl().goods_style,
		brand:common.Geturl().brand,
		prodaddress:common.Geturl().prodaddress,
		token:token,
		page: testn,
		pagelimit:30
	},
	ajax:function(){
		var people_style=common.Geturl().people_style,
			goods_style=common.Geturl().goods_style,
			token=token;
		ordercloum=$("#sortNav li").eq(0).attr("attrc");
		ObjFun.goodsListObj.ordercloum=ordercloum;

		//console.log(ObjFun.goodsListObj)
		common.ajaxFun(ObjFun.Interface.goodsDaohang,"POST",{people_style:people_style,token:token},ObjFun.DaohangFun);   //导航
		common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun);   //商品
	},
	DetailsFun:function(){
		$("#clothesMain").on("click","li",function(){
			var goods_id=$(this).attr("goods_id");
			window.location.href="../page/CommodityDetails.html?goods_id="+goods_id;
		})
	},
	DaohangFun:function(res){
		ObjFun.initdaohang(res);
		ObjFun.intiChange();
	},
	initdaohang:function(res){
		var Str="";
		var brands=res.data.brands,goodstyle=res.data.goodstyle,peopletype=res.data.peopletype,prodaddress=res.data.shipprovince_address;
		function daohanglist(data,ele,key){
			var Str="";
			if (data==null) {
				Str="";
			}else if (data!=null) {
				$.each(data,function(i,v){
					Str+='<span attrId='+i+' key='+key+'>'+v+'</span>'
				});
			};
			ele.html(Str);
		}
		daohanglist(brands,$("#brands"),"brand")//品牌
		daohanglist(goodstyle,$("#goodstyle"),"goods_style")//风格
		daohanglist(peopletype,$("#peopletype"),"people_style")//适应人群
		daohanglist(prodaddress,$("#prodaddress"),"prodaddress")//地址
	},
	intiChange:function(){
		$("#goodstyle span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().goods_style) {
				$(".goodstyle").show();
				$(".goodstyle span").eq(0).text($(this).text());
			};
		});

		$("#peopletype span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().people_style) {
				$(".peopletype").show();
				$(".peopletype span").eq(0).text($(this).text());
			};
		})
		$("#brands span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().brand) {
				$(".brands").show();
				$(".brands span").eq(0).text($(this).text());
			};
		})
		$("#prodaddress span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().prodaddress) {
				$(".prodaddress").show();
				$(".prodaddress span").eq(0).text($(this).text());
			};
		})
	},
	changeUrl:function(attrs,attrVal,peoples){
		//console.log(attrs)
		var newGetUrlParam= common.GetUrlParam();
		var origin_href=window.location.href.split("?")[0];
		//newGetUrlParam["pageNumber"]=1;
		var strGetUrlParam="";
		newGetUrlParam[attrs]=attrVal;
		if (peoples==1) {
			strGetUrlParam=attrs+"="+attrVal;//+"&pageNumber=1";
		}else {
			$.each(newGetUrlParam,function(i,v){
				strGetUrlParam+=i+"="+v+"&"
			});
			strGetUrlParam=strGetUrlParam.substring(0,strGetUrlParam.length-1);
		};
		window.location.href=origin_href+"?"+strGetUrlParam;
	},
	DaohangeventFun:function(){
			var flag=1;
				ObjFun.goodsListObj.people_style=common.Geturl().people_style;
				//goodsListObj.goods_style=common.Geturl().goods_style;

				//delete ObjFun.goodsListObj.goods_style;
				ObjFun.goodsListObj.token=token;
				ObjFun.goodsListObj.ordercloum=$("#sortNav li").eq(0).attr("attrC");
		$(".mores").on("click",function(){
			$(this).parents("li").toggleClass("open");
		});
		

		$("#peopletype").on("click","span",function(){
			ObjFun.goodsListObj.people_style=$(this).attr("attrid");
			delete ObjFun.goodsListObj.goods_style;
			txt=$(this).text();
			$(".peopletype").show().children("span").eq(0).text(txt);
			$(".goodstyle").hide();
			ObjFun.changeUrl($(this).attr("key"),$(this).attr("attrid"),1)

		common.ajaxFun(ObjFun.Interface.goodsDaohang,"POST",{people_style:$(this).attr("attrid"),token:token},ObjFun.DaohangFun);   //导航 适应人群    

		//common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun);   //适应人群
		});
		$("#goodstyle").on("click","span",function(){
			ObjFun.goodsListObj.goods_style=$(this).attr("attrid");
			txt=$(this).text();
			$(".goodstyle").show().children("span").eq(0).text(txt);
			ObjFun.changeUrl($(this).attr("key"),$(this).attr("attrid"));

		//common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun);   //风格
		});
		$("#brands").on("click","span",function(){
			ObjFun.goodsListObj.brand=$(this).attr("attrid");
			txt=$(this).text();
			$(".brands").show().children("span").eq(0).text(txt);
			ObjFun.changeUrl($(this).attr("key"),$(this).attr("attrid"));
		//common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun);   //品牌
		});
		$("#prodaddress").on("click","span",function(){
			ObjFun.goodsListObj.prodaddress=$(this).attr("attrid");
			txt=$(this).text();
			$(".prodaddress").show().children("span").eq(0).text(txt);
			ObjFun.changeUrl($(this).attr("key"),$(this).attr("attrid"));

		//common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun);   //产地
		});
		
		$(".close").on("click",function(){
			var deAttr=$(this).parent("li").attr("attra");
			$(this).parent("li").hide();
			//console.log(goodsListObj),page:1,pagelimit:2
			delete ObjFun.goodsListObj[deAttr];
			var newGetUrlParam= common.GetUrlParam();
			var origin_href=window.location.href.split("?")[0];
			//newGetUrlParam["pageNumber"]=1;
			var strGetUrlParam="";
			delete newGetUrlParam[deAttr];

				$.each(newGetUrlParam,function(i,v){
					strGetUrlParam+=i+"="+v+"&"
				});
				strGetUrlParam=strGetUrlParam.substring(0,strGetUrlParam.length-1);

			window.location.href=origin_href+"?"+strGetUrlParam;

		//common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun());  //关闭 
			
		});

		$("#sortNav").on("click","li.lis",function(){
			$(this).addClass("on").siblings("li").removeClass("on");
			ObjFun.goodsListObj.ordercloum=$(this).attr("attrc");
			if (ObjFun.goodsListObj.ordercloum==4) {
				if (flag==1) {
					$(".price").addClass("priceUp").removeClass("priceDown");
					flag=0;
					ObjFun.goodsListObj.ordertype="desc";
				}else if (flag==0) {
					$(".price").addClass("priceDown").removeClass("priceUp");
					flag=1;
					ObjFun.goodsListObj.ordertype="asc";
				};
				
			};
			testn=1;
			ObjFun.goodsListObj.page=1;
		common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun);   //产地

		});
		$("#region1 p").click(function(){
			$("#regionList1").show();
			$("#provinceList1").hide();
			$(this).addClass("open");
		})
		$("#province1 p").click(function(){
			common.ajaxFun(commonFunObj.Interface.getProvince,"POST",{},function(res){
				var strProv="",v;
				$.each(res.data,function(k,v){
					strProv+="<div id="+k+">"+v+"</div>";
				})
				$("#provinceList1").html(strProv);
					$("#provinceList1").show();
					$("#regionList1").hide();
					$(this).addClass("open");
				
				});
			}); 
				$("#provinceList1").on("click","div",function(){
					va=$(this).attr("id");
					ObjFun.goodsListObj.province=va;
					$("#region1 p").html("请选择市").addClass("open");
					$("#province1 p").attr("value",va);
					$("#province1 p").html($(this).text()).removeClass("open");
					
					$(this).addClass("on").siblings("div").removeClass("on");
					$("#provinceList1").hide();
					testn=1;
					ObjFun.goodsListObj.page=1;
		common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun);   //省份

					common.ajaxFun(commonFunObj.Interface.getCity,"POST",{provinceid:va},function(res){
						$("#regionList1").show();
						var strCity="",v;
							$.each(res.data,function(k,v){
								strCity+="<div id="+k+">"+v+"</div>";
							});
							$("#regionList1").html(strCity);
						}); 
				});
				$("#regionList1").on("click","div",function(){
					$("#region1 p").html($(this).text()).removeClass("open");
					$("#region1 p").attr("value",$(this).attr("id"));
					ObjFun.goodsListObj.city=$(this).attr("id");
					$("#regionList1").hide();
					$(this).addClass("on").siblings("div").removeClass("on");
					testn=1;
					ObjFun.goodsListObj.page=1;
					common.ajaxFun(ObjFun.Interface.goodsList,"POST",ObjFun.goodsListObj,ObjFun.goodsListFun);   //城市
				});
	},
	goodsListFun:function(res){
		//console.log(res.data)
			if (res.code==1) {
				str="";
			}else {
			var str="";
				$.each(res.data,function(i,v){
					if (typeof v=="object") {
					str+='<li goods_style='+v.goods_style+' goods_id='+v.goods_id+'>'+
						'<div class="imgBox" people_style='+v.people_style+'><img src='+v.image+' alt=""></div>'+
						'<div class="prices">¥'+v.min_price+'</div>'+
						'<div class="introduce">'+v.goods_name+'</div>'+
						'<div class="company" seller_id='+v.seller_id+' brand='+v.brand+'>'+v.seller_name+'</div>'+
						'<div class="productionAddress" prodaddress='+v.prodaddress+'>'+v.shipaddress_name+'</div>'+
					'</li>';
					}
				});
			};
			$("#clothesMain ul").html(str);
			//console.log(ObjFun.pageFlag);
				ObjFun.pageFun(res,ObjFun.goodsListObj);
		},
	pageFun:function(res,Obj){
		var pagelimit=Obj.pagelimit;//每页条数
		//console.log(res.data.count + "totalAcounts")
		pageinit(res.data.count,Math.ceil(res.data.count/pagelimit));
		
		function pageinit(totalRecords,totalPage){

			var totalPage = totalPage;//总页数
			var totalRecords = totalRecords;    //总数据条数
			var pageNumber = testn;

			if(!testn){
				pageNumber = 1;
			}
			console.log("总数据条数："+totalRecords+"总页数："+totalPage)
			//生成分页
			//有些参数是可选的，比如lang，若不传有默认值
			kkpager.generPageHtml({
				pno : pageNumber,
				//总页码
				total : totalPage,
				//总数据条数
				totalRecords : totalRecords,
				isGoPage 			: false,	//是否显示页码跳转输入框
				isWrapedPageBtns	: false,	//是否用span包裹住页码按钮
				isShowFirstPageBtn	: false, //是否显示首页按钮
				isShowLastPageBtn	: false, //是否显示尾页按钮
				mode : 'click',//默认值是link，可选link或者click
				click : function(n){
					testn = n;
					Obj.page=n;
					console.log(testn)
					common.ajaxFun(ObjFun.Interface.goodsList,"POST",Obj,function(res){
						ObjFun.goodsListFun(res);
					}); 
					this.selectPage(n);
					return false;
				}
			},true);
		};
	}
}
ObjFun.inits();